package net.wavorizer.util;

public class Utils
{
    public static String remNull(String s)
    {
        return s == null ? "" : s;
    }

    public static byte[] removeBOM(byte [] bom)
    {
        int unread = -1;

        if (bom.length > 2 && bom[0] == (byte) 0xEF && bom[1] == (byte) 0xBB && bom[2] == (byte) 0xBF)
        {
            //"UTF-8";
            unread = 3;
        }
        else if (bom.length > 1 && bom[0] == (byte) 0xFE && bom[1] == (byte) 0xFF)
        {
            //"UTF-16BE";
            unread = 2;
        }
        else if (bom.length > 1 && bom[0] == (byte) 0xFF && bom[1] == (byte) 0xFE)
        {
            //"UTF-16LE";
            unread = 2;
        }
        else if (bom.length > 3 && bom[0] == (byte) 0x00 && bom[1] == (byte) 0x00 && bom[2] == (byte) 0xFE && bom[3] == (byte) 0xFF)
        {
            //"UTF-32BE";
            unread = 4;
        }
        else if (bom.length > 3 && bom[0] == (byte) 0xFF && bom[1] == (byte) 0xFE && bom[2] == (byte) 0x00 && bom[3] == (byte) 0x00)
        {
            //"UTF-32LE";
            unread = 4;
        }
        if(unread != -1)
        {
            byte[] finalByteMsg = new byte[bom.length - unread];
            System.arraycopy(bom, 2, finalByteMsg, 0, finalByteMsg.length);
            return finalByteMsg;
        }
        else
            return bom;
    }


}
